<template>
  <a-layout style="height: 100vh; overflow: hidden">
    <a-layout-header class="header">
      <div class="header-left">Logo</div>
      <div class="header-right">
        <a-space>
          <a-button type="text" @click="goToFrontend">前台</a-button>
          <a-button type="text" @click="logout">
            退出
            <icon-export />
          </a-button>
        </a-space>
      </div>
    </a-layout-header>
    <a-layout>
      <a-layout-sider class="sider" :collapsible="true" breakpoint="xl">
        <a-menu
          @menu-item-click="handerMenuItemClick"
          :style="{ width: '100%', height: '100%' }"
          :default-open-keys="['BlogManagement']"
          :default-selected-keys="['BackendBlogList']"
          breakpoint="xl"
          @collapse="onCollapse"
        >
          <a-sub-menu key="BlogManagement">
            <template #icon>
              <icon-apps></icon-apps>
            </template>
            <template #title>文章管理</template>
            <a-menu-item key="BackendBlogList">文章列表</a-menu-item>
            <a-menu-item key="BackendTagList">标签列表</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="CommentManagement">
            <template #icon> <icon-message /></template>
            <template #title>评论管理</template>
            <a-menu-item key="BackendCommentList">评论列表</a-menu-item>
          </a-sub-menu>
        </a-menu>
        <template #trigger="{ collapsed }">
          <icon-menu-unfold v-if="collapsed" />
          <icon-menu-fold v-else />
        </template>
      </a-layout-sider>
      <a-layout-content class="page">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { LOGOUT } from '@/api/vblog'
import app from '@/stores/app'

export default {
  setup() {
    const router = useRouter()
    const loading = ref(false) // 添加 loading 状态

    const logout = async () => {
      try {
        loading.value = true
        await LOGOUT(app.value.token.refresh_token)
      } finally {
        loading.value = false
      }
      router.push({
        name: 'LoginPage'
      })
    }

    const goToFrontend = () => {
      router.push({ name: 'FrontendBlogList' }) // 确保使用正确的路由名称
    }

    const handerMenuItemClick = (v) => {
      router.push({ name: v }) // 根据 key 跳转到相应的路由
    }

    const onCollapse = (v) => {
      console.log(v)
    }

    return {
      logout,
      goToFrontend, // 返回 goToFrontend 方法
      handerMenuItemClick,
      onCollapse
    }
  }
}
</script>

<style lang="css" scoped>
.header {
  background-color: #fff;
  height: 59px;
  border-bottom: 1px solid #e8e8e8;
  padding: 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sider {
  height: calc(100vh - 60px);
}

.header-left {
}

.header-right {
  display: flex;
  align-items: center;
}

.to-frontend {
}

.logout {
}

.page {
  padding: 6px;
  overflow: auto;
  height: calc(100vh - 60px);
  background-color: #f0f0f0;
}

.page::-webkit-scrollbar {
  display: none;
}
</style>
